import mx.events.EventDispatcher;
import mx.utils.Delegate;

import org.molamil.acto.core.EventConstants;
import org.molamil.acto.core.Logger;
import org.molamil.acto.pages.PageRequest;
import org.molamil.acto.pages.PagesConf;
import org.molamil.acto.pages.controller.AbstractPagesController;
import org.molamil.acto.pages.history.HistoryManager;
import org.molamil.acto.pages.history.HistoryStack;
import org.molamil.acto.pages.master.PageMaster;

/**
 * @author Jorge Hernandez
 */
class org.molamil.acto.pages.history.BasicHistoryManager extends HistoryStack implements HistoryManager {

	
	// Properties
	
	private static var log:Logger = Logger.getLogger("BasicHistoryManager");
		private var pagesController:AbstractPagesController;
			// Constructor
	
	public function BasicHistoryManager(pagesController:AbstractPagesController) {
		log.debug("Constructor");
		EventDispatcher.initialize(this);
		this.pagesController = pagesController;
		pagesController.addEventListener(EventConstants.PAGE_DISPLAY, Delegate.create(this, onPageDisplay));
	}

	
	// Functions
	
	public function dispatchEvent(eventObj:Object):Void {}
	public function addEventListener(event:String, handler):Void {}
	public function removeEventListener(event:String, handler):Void {}
	
	public function back():PageMaster {
		
		var master:PageMaster = null;
		
		if (_index > 0) {
			
			_index--;
			var request:PageRequest = _list[_index];
			log.debug("back(), name = " + request.name);
			var params:Object = request.params != null ? request.params : new Object();
			params[PagesConf.HISTORY_PARAM_NAME] = PagesConf.HISTORY_PARAM_VALUE_BACK;			master = pagesController.goto(request.name, params);
			
			var eventObject:Object = {target: this, type: EventConstants.HISTORY_BACK, request: request};
			dispatchEvent(eventObject);
			
		} else {
			log.debug("Can't go back()!");
		}
		
		return master;
		
	}
	
	public function forward():PageMaster {
		
		var master:PageMaster = null;
		
		if (_index < _list.length - 1) {
			
			_index++;
			var request:PageRequest = _list[_index];
			log.debug("forward(), name = " + request.name);
			var params:Object = request.params != null ? request.params : new Object();
			params[PagesConf.HISTORY_PARAM_NAME] = PagesConf.HISTORY_PARAM_VALUE_FORWARD;
			master = pagesController.goto(request.name, params);
			
			var eventObject:Object = {target: this, type: EventConstants.HISTORY_FORWARD, request: request};
			dispatchEvent(eventObject);
			
		} else {
			log.debug("Can't go forward()!");
		}
		
		return master;
		
	}
	
	public function getHistory():HistoryStack {
		return new HistoryStack(_list, _index);
	}
	
	private function onPageDisplay(eventObject:Object):Void {
		var master:PageMaster = eventObject.target;		if (master.getRequest().params[PagesConf.HISTORY_PARAM_NAME] == null) {
			log.debug("Adding page to history: " + master.getContext().name);
			while (_list.length > _index + 1)
				_list.pop();
			_index++;
			_list.push(master.getRequest());
		}
	}
	
	
}
